package com.example.exercise.controller;

import com.example.exercise.common.BaseResponse;
import com.example.exercise.jasper.JasperUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/jasper")
public class JasperReportController {
    @Autowired
    private DataSource dataSource;

    @RequestMapping(value = "/pdfJavaShow")
    public BaseResponse<?> pdfJavaShow(HttpServletResponse httpServletResponse){
        BaseResponse<?> baseResponse = new BaseResponse<>();
        String filePath = Thread.currentThread().getContextClassLoader().getResource("/demo1.jasper").getPath();
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("name", "xiaoming");
        map1.put("age", 20);
        map1.put("price", 99999000);
        list.add(map1);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", "xiaohong");
        map2.put("age", 55);
        map2.put("price", 9990);
        list.add(map2);
        Map<String, Object> map = new HashMap<>();
        map.put("date", "2022-07-14");
        JasperUtils.showPdfByJavaBean(httpServletResponse, list, filePath, map);
        baseResponse.ok();
        return baseResponse;
    }

    @RequestMapping(value = "/excelJavaOut")
    public BaseResponse<?> excelJavaOut(HttpServletResponse httpServletResponse) {
        String filePath = Thread.currentThread().getContextClassLoader().getResource("/demo2.jasper").getPath();
        BaseResponse<?> baseResponse = new BaseResponse<>();
        Map<String, Object> map = new HashMap<>();
        map.put("date", "2022-07-14");
        JasperUtils.showPdfByJdbc(httpServletResponse, dataSource, filePath, map);
        baseResponse.ok();
        return baseResponse;
    }

}
